package com.stu.xml;

import com.alibaba.fastjson.JSONObject;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.jupiter.api.Test;

/**
 * @author zhuzi
 * https://www.cnblogs.com/liwanliangblog/p/15683939.html
 * https://www.cnblogs.com/cnsdhzzl/p/8390514.html
 * <p>
 * xlms
 * http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
 */
public class Demo {

    @Test
    public void xml() throws JAXBException {
        LiwanLiangJAXB liwanLiangJAXB = new LiwanLiangJAXB();
        liwanLiangJAXB.setName("xml");
        liwanLiangJAXB.setAge(29);
        liwanLiangJAXB.setId(1);

        JAXBContext jaxbContext = JAXBContext.newInstance(LiwanLiangJAXB.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);

        jaxbMarshaller.marshal(liwanLiangJAXB,System.out);
    }

    @Data
    @XmlRootElement
    @XmlType(namespace = "namespace.com")
    public static class LiwanLiangJAXB {

        private String name;
        private int age;
        private int id;

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this,ToStringStyle.NO_CLASS_NAME_STYLE);
        }
    }

    @Test
    public void jsonXml() {
        String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<Demo>\n" + "  <id>1</id>\n" + "  <name>demo</name>\n" + "</Demo>";
        JSONObject jsonObject = new JSONObject();

        jsonObject.fluentPut("msg",str);
        System.out.println(jsonObject);
    }
}
